package net.sx.minemodule.mvp.activities;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.homegoods.aroutercomment.ARouterPath;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.util.ToastUtil;

import net.sx.minemodule.R;
import net.sx.minemodule.databinding.MineActivityRefundInputLogisticsBinding;
import net.sx.minemodule.mvp.bean.LogisticsCompany;
import net.sx.minemodule.mvp.bean.RefundBean;
import net.sx.minemodule.mvp.presenters.MineRefundLogisticsPresenter;
import net.sx.minemodule.mvp.views.MineRefundInputLogisticsView;
import net.sx.minemodule.widget.LogisticsSelectDialog;

import java.util.List;

/**
 * 填写物流单号
 * 冯鑫林 2019-7-6
 */
@Route(path = ARouterPath.PATH_MINE_AFTER_DETAIL_LOGISTICS)
public class MineRefundInputLogisticsActivity extends MyBaseFragmentActivity<MineActivityRefundInputLogisticsBinding, MineRefundLogisticsPresenter>
        implements MineRefundInputLogisticsView {

    private LogisticsSelectDialog logisticsSelectDialog;
    private LogisticsCompany selectedCompany;
    private RefundBean refundBean;

    @Override
    protected int getLayout() {
        return R.layout.mine_activity_refund_input_logistics;
    }

    @Override
    protected void initEvent() {
        setTitle("填写退货物流");
        refundBean = (RefundBean) getIntent().getSerializableExtra("refundBean");
        mBinding.setViewmodel(this);

    }

    /**
     * 点击提交当前物流信息
     */
    public void onCommit(View v) {
        String orderNumber = mBinding.mineEtInputLogisticsOrder.getText().toString();
        if (selectedCompany == null) {
            ToastUtil.showToast(getString(R.string.mine_select_logistics_company));
            return;
        }
        if (TextUtils.isEmpty(orderNumber)) {
            ToastUtil.showToast(getString(R.string.mine_input_logistics_number));
            return;
        }
        selectedCompany.setOrderNumber(orderNumber);
        selectedCompany.setReturnApplyId(refundBean.getId());
        showLoad("提交中，请稍候...");
        getPresenter().commitLogisticsInfo(selectedCompany);
    }

    /**
     * 获取物流公司列表
     *
     * @param view
     */
    public void onSelectLogisticsCompany(View view) {
        if (logisticsSelectDialog == null) {
            showLoad("正在获取数据...");
            getPresenter().getLogisticsCompanys();
        } else {
            logisticsSelectDialog.show();
        }
    }


    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
    }

    @Override
    protected MineRefundLogisticsPresenter getPresenter() {
        return new MineRefundLogisticsPresenter(this);
    }

    @Override
    public void getLogisticsCompanySuccess(List<LogisticsCompany> data) {
        hideLoad();
        logisticsSelectDialog = new LogisticsSelectDialog(this, data, company -> {
            selectedCompany = company;
            if (company != null) {
                mBinding.mineTvInputLogisticsName.setText(company.getName());
            }
        });
        logisticsSelectDialog.show();
    }

    @Override
    public void getLogisticsCompanyFail(String message) {
        hideLoad();
        ToastUtil.showToast(message);
    }

    @Override
    public void commitSuccess() {
        hideLoad();
        //前面的页面刷新状态
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void commitFail(String message) {
        hideLoad();
        ToastUtil.showToast(message);
    }

}